<?php
    class TaskController extends Controller {
        public $metaDescription = null;
        public $metaKeywords = null;
        public $linkCanoncical = null;
        public $title = null;
        public $layout = "column_project";

        public function actionIndex(){
            //$this->layout='column_project';        
            $url = new Url();     
            $project_id = $_SESSION['project_pm']['project_id'];
            $user_id = $_SESSION['user_pm']['user_id'];
            $status_id = isset($_GET["status_id"])?mysql_escape_string($_GET["status_id"]):0;
            $data = Task::getRowByProjectId($project_id,$status_id);        
            $data_task_user = Task::getRowByUserId($user_id);            
            $this->render('index',array("data"=>$data,"data_task_user"=>$data_task_user));
        }

        public function actionDetail(){                
            $url = new Url();     
            $task_id = isset($_GET["task_id"])?mysql_escape_string($_GET["task_id"]):0;
            $task_data = Task::getRowById($task_id);    

            $assign_data = Assign::getRowsByTaskID($task_id);
            
            $log_data = Log::getRowByTaskID($task_id);            

            $this->render('detail',array("task_data"=>$task_data,"assign_data"=>$assign_data,"log_data"=>$log_data));
        }

        public function actionLogtime(){
            //$this->layout='column_project';        
            $url = new Url();     
            $task_id = isset($_GET["task_id"])?mysql_escape_string($_GET["task_id"]):0;
            $task_data = Task::getRowById($task_id);        

            $this->render('log_time',array("task_data"=>$task_data));
        }

        public function actionAdd(){                
            $url = new Url();     
            $project_id = $_SESSION['project_pm']['project_id'];
            $data_category = Category::getRowByProjectID($project_id);        
            $data_version = Version::getRowByProjectID($project_id);        
            $data_member = Member::getRowByProjectId($project_id);        
           
           /*echo "<pre>";
                print_r($data_member);
                echo "</pre>";
                die; */
                        
            $this->render('add',array("data_category"=>$data_category,"data_version"=>$data_version,"data_member"=>$data_member));
        }
        
        public function actionUpdate(){
            $url = new Url();     
            $project_id = $_SESSION['project_pm']['project_id'];
            $task_id = isset($_GET["task_id"])?mysql_escape_string($_GET["task_id"]):0;
            $data_category = Category::getRowByProjectID($project_id);        
            $data_version = Version::getRowByProjectID($project_id);        
            $data_member = Member::getRowByProjectId($project_id);               
            $data_task = Task::getRowById($task_id);
            $data_assign = Assign::getRowsByTaskID($task_id);
            $this->render('update',array("data_category"=>$data_category,"data_version"=>$data_version,"data_member"=>$data_member,"data_task"=>$data_task,"data_assign"=>$data_assign));
        }

        public function actionAjaxAddTask(){            

            $project_id = $_SESSION['project_pm']['project_id'];
            $user_id = $_SESSION['user_pm']['user_id'];
            $category_id = isset($_GET["category_id"])?mysql_escape_string($_GET["category_id"]):"";
            $task_name = isset($_GET["task_name"])?mysql_escape_string($_GET["task_name"]):"";
            $task_description = isset($_GET["task_description"])?mysql_escape_string($_GET["task_description"]):"";
            $task_status = isset($_GET["task_status"])?mysql_escape_string($_GET["task_status"]):"";            
            $task_priority = isset($_GET["task_priority"])?mysql_escape_string($_GET["task_priority"]):"";                    
            $task_version = isset($_GET["task_version"])?mysql_escape_string($_GET["task_version"]):"";            
            $start_date = isset($_GET["start_date"])?mysql_escape_string($_GET["start_date"]):"";            
            $end_date = isset($_GET["end_date"])?mysql_escape_string($_GET["end_date"]):"";            
            $task_estimated_time = isset($_GET["task_estimated_time"])?mysql_escape_string($_GET["task_estimated_time"]):"";            
            $task_percent_done = isset($_GET["task_percent_done"])?mysql_escape_string($_GET["task_percent_done"]):"";            
            $assign_user_id = isset($_GET["assign_user_id"])?mysql_escape_string($_GET["assign_user_id"]):"";                    
            //$selected = isset($_GET["selected"])?mysql_escape_string($_GET["selected"]):"";
            $array_user_id = split(',',$assign_user_id);        

            $array_input['project_id'] = $project_id;
            $array_input['task_name'] = $task_name;
            $array_input['task_description'] = $task_description;
            $array_input['task_status'] = $task_status;
            $array_input['task_priority'] = $task_priority;
            $array_input['task_category'] = $category_id;
            $array_input['task_version'] = $task_version;
            $array_input['task_start_date'] = strtotime(DateUtils::convertDatedb($start_date));
            $array_input['task_end_date'] = strtotime(DateUtils::convertDatedb($end_date));
            $array_input['task_estimated_time'] = $task_estimated_time;
            $array_input['task_percent_done'] = $task_percent_done;
            $array_input['task_create_user'] = $user_id;
            $array_input['task_update_date'] = time();
            $array_input['task_create_date'] = time();

            $last_task_id = Task::insertObject($array_input);        

            /*$array_input = array();
            $array_input['task_id'] = $last_task_id;
            $array_input['assign_user_id'] = $assign_user_id;

            $last_id = Assign::insertObject($array_input);*/

            foreach($array_user_id as $row){
                $array_input = array();
                $array_input['task_id'] = $last_task_id;                    
                $array_input['assign_user_id'] = $row;                   
                $last_id = Assign::insertObject($array_input);
            }        
            
            $data_assign = Assign::getRowsByTaskID($last_task_id);
            $project_name = $_SESSION['project_pm']['project_name'];
            foreach($data_assign as $row){
                $messege = "Bạn có việc mới từ dự án $project_name: $task_name. <br> Nội dung công việc : $task_description. <br> Thời gian hoàn thành : $start_date - $end_date";
                $mail = $row['email'];                
                SendMail::sendMail($mail,$messege);
            }

            $array_activity['project_id'] = $project_id;
            $array_activity['activity_user_id'] = $user_id;
            $array_activity['activity_name'] = "Công việc #$last_id (tạo mới) : $task_name";
            $array_activity['activty_type'] = 2;
            $array_activity['activity_update_time'] = time();        
            $log = Activity::insertObject($array_activity);

            if($last_id !=0){            
                echo 1;                                           
            }else{
                echo 0;
            }
        }

        public function actionAjaxUpdateTask(){            

            $project_id = $_SESSION['project_pm']['project_id'];
            $user_id = $_SESSION['user_pm']['user_id'];
            $task_id = isset($_GET["task_id"])?mysql_escape_string($_GET["task_id"]):"";
            $category_id = isset($_GET["category_id"])?mysql_escape_string($_GET["category_id"]):"";
            $task_name = isset($_GET["task_name"])?mysql_escape_string($_GET["task_name"]):"";
            $task_description = isset($_GET["task_description"])?mysql_escape_string($_GET["task_description"]):"";
            $task_status = isset($_GET["task_status"])?mysql_escape_string($_GET["task_status"]):"";            
            $task_priority = isset($_GET["task_priority"])?mysql_escape_string($_GET["task_priority"]):"";                    
            $task_version = isset($_GET["task_version"])?mysql_escape_string($_GET["task_version"]):"";            
            $start_date = isset($_GET["start_date"])?mysql_escape_string($_GET["start_date"]):"";            
            $end_date = isset($_GET["end_date"])?mysql_escape_string($_GET["end_date"]):"";            
            $task_estimated_time = isset($_GET["task_estimated_time"])?mysql_escape_string($_GET["task_estimated_time"]):"";            
            $task_percent_done = isset($_GET["task_percent_done"])?mysql_escape_string($_GET["task_percent_done"]):"";            
            $assign_user_id = isset($_GET["assign_user_id"])?mysql_escape_string($_GET["assign_user_id"]):"";                    
            //$selected = isset($_GET["selected"])?mysql_escape_string($_GET["selected"]):"";
            $array_user_id = split(',',$assign_user_id);        

            $array_input['project_id'] = $project_id;
            $array_input['task_name'] = $task_name;
            $array_input['task_description'] = $task_description;
            $array_input['task_status'] = $task_status;
            $array_input['task_priority'] = $task_priority;
            $array_input['task_category'] = $category_id;
            $array_input['task_version'] = $task_version;
            $array_input['task_start_date'] = strtotime(DateUtils::convertDatedb($start_date));
            $array_input['task_end_date'] = strtotime(DateUtils::convertDatedb($end_date));
            $array_input['task_estimated_time'] = $task_estimated_time;
            $array_input['task_percent_done'] = $task_percent_done;
            $array_input['task_create_user'] = $user_id;
            $array_input['task_update_date'] = time();
            $array_input['task_create_date'] = time();

            $last_task_id = Task::updateObject($array_input,"task_id",$task_id);

            /*$array_input = array();
            $array_input['task_id'] = $last_task_id;
            $array_input['assign_user_id'] = $assign_user_id;

            $last_id = Assign::insertObject($array_input);*/
            $array_delete['task_id'] = $task_id;
            Assign::deleteObject($array_delete);
            
            foreach($array_user_id as $row){
                $array_input = array();
                $array_input['task_id'] = $task_id;                    
                $array_input['assign_user_id'] = $row;                   
                $last_id = Assign::insertObject($array_input);
            }        
            
            $project_name = $_SESSION['project_pm']['project_name'];
            $messege = "Việc mới từ dự án $project_name : $task_name đã được cập nhật. <br> Nội dung công việc : $task_description. <br> Thời gian hoàn thành : $start_date - $end_date";
            $mail = $_SESSION['project_pm']['email'];                
            SendMail::sendMail($mail,$messege);
                       
            $array_activity['project_id'] = $project_id;
            $array_activity['activity_user_id'] = $user_id;
            $array_activity['activity_name'] = "Công việc #$last_id (cập nhật) : $task_name";
            $array_activity['activty_type'] = 2;
            $array_activity['activity_update_time'] = time();        
            $log = Activity::insertObject($array_activity);

            if($last_id !=0){            
                echo 1;                                           
            }else{
                echo 0;
            }
        }
        
        public function actionAjaxAddLog(){            

            $project_id = $_SESSION['project_pm']['project_id'];
            $user_id = $_SESSION['user_pm']['user_id'];
            $user_name = $_SESSION['user_pm']['username'];
            $task_id = isset($_GET["task_id"])?mysql_escape_string($_GET["task_id"]):"";
            $log_date = isset($_GET["log_date"])?mysql_escape_string($_GET["log_date"]):"";
            $log_hours = isset($_GET["log_hours"])?mysql_escape_string($_GET["log_hours"]):"";
            $log_description = isset($_GET["log_description"])?mysql_escape_string($_GET["log_description"]):"";                    
            $task_percent_done = isset($_GET["task_percent_done"])?mysql_escape_string($_GET["task_percent_done"]):"";           
            $task_status = 1;
            if($task_percent_done > 0 && $task_percent_done != 100){
                $task_status = 2;
            }
            if($task_percent_done == 100){
                $task_status = 3;
            }
            
            $array_input['task_id'] = $task_id;
            $array_input['user_id'] = $user_id;        
            $array_input['log_date'] = strtotime(DateUtils::convertDatedb($log_date));        
            $array_input['log_hour'] = $log_hours;
            $array_input['log_description'] = $log_description;                
            $array_input['log_create_date'] = time();        
            $last_id = Log::insertObject($array_input);        

            $array_task['task_percent_done'] = $task_percent_done;
            $array_task['task_status'] = $task_status;
            $temp = Task::updateObject($array_task,"task_id",$task_id);
            
            $array_activity['project_id'] = $project_id;
            $array_activity['activity_user_id'] = $user_id;
            $array_activity['activity_name'] = "Công việc #$task_id : $user_name báo cáo thời gian làm việc";
            $array_activity['activty_type'] = 4;
            $array_activity['activity_update_time'] = time();        
            $log = Activity::insertObject($array_activity);    

            $project_name = $_SESSION['project_pm']['project_name'];
            //$messege = "Công việc #$task_id của dự án $project_name. <br> Công việc được $user_name cập nhật tiến độ.";
            $messege = "Cong viec #$task_id cua du an $project_name. <br> Cong viec duoc $user_name cap nhat tien do.";
            $mail = $_SESSION['project_pm']['email']; 
            SendMail::sendMail($mail,$messege);
            
            if($last_id !=0){            
                echo 1;                                           
            }else{
                echo 0;
            }
        }

        public function actionAjaxDeleteTask(){            

            $project_id = $_SESSION['project_pm']['project_id'];        
            $user_id = $_SESSION['user_pm']['user_id'];
            $task_id = isset($_GET["task_id"])?mysql_escape_string($_GET["task_id"]):"";

            $array_input['task_id'] = $task_id;        
            $last_id = Task::deleteObject($array_input);

            $array_activity['project_id'] = $project_id;
            $array_activity['activity_user_id'] = $user_id;
            $array_activity['activity_name'] = "Công việc #$task_id (xoá)";
            $array_activity['activty_type'] = 4;
            $array_activity['activity_update_time'] = time();        
            $log = Activity::insertObject($array_activity);    

            if($last_id !=0){            
                echo 1;                                           
            }else{
                echo 0;
            }
        }

        public function actionSendMail(){
            echo SendMail::sendMail();die;
            require_once('class.phpmailer.php');
            $name = "Nguyen abc";
            $message = "danh nhau de";
            $sender = "coldboy89@gmail.com";
            $receiver = "coldboy89abc@gmail.com";
            $subject = "subject abc";

            {
                $mail = new PHPMailer();
                $mail->IsSMTP(); // telling the class to use SMTP
                //$mail->Host       = "mail.icoldstyle.com"; // SMTP server
                //$mail->SMTPDebug  = 2;                     // enables SMTP debug information (for testing)
                // 1 = errors and messages
                // 2 = messages only
                $mail->SMTPAuth   = true;                  // enable SMTP authentication
                //$mail->SMTPSecure = "ssl";                 // sets the prefix to the servier
                $mail->Host       = "smtp.gmail.com";      // sets GMAIL as the SMTP server
                $mail->Port       = 465;                   // set the SMTP port for the GMAIL server
                $mail->Username   = "coldboy89@gmail.com";  // GMAIL username
                $mail->Password   = "shinhip215";            // GMAIL password

                $mail->SetFrom($sender, $sender);

                $mail->AddReplyTo($sender,$sender);

                $mail->Subject    = "Mail nhac viec tu iColdStyle PM";

                //$mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test

                $mail->MsgHTML($message);
                //$mail->Send();        
                $address = "coldboy89@gmail.com";
                $mail->AddAddress($address, "Nguyen Tuan Anh");

                //$mail->AddAttachment("images/phpmailer.gif");      // attachment
                //$mail->AddAttachment("images/phpmailer_mini.gif"); // attachment

                if(!$mail->Send()) {
                    echo "Mailer Error: " . $mail->ErrorInfo;
                } else {
                    echo "Message sent!";
                }
            }

        }

    }

?>
